/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.message.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.modules.message.dao.StoreEmployeeTaskDao;
import com.chlitina.store.modules.message.entity.StoreEmployeeTask;

/**
 * 员工任务单Service
 * 
 * @author lvbo
 * @version 2013-11-09
 */
@Component
@Transactional(readOnly = true)
public class StoreEmployeeTaskService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory
			.getLogger(StoreEmployeeTaskService.class);

	@Autowired
	private StoreEmployeeTaskDao storeEmployeeTaskDao;

	public StoreEmployeeTask get(Long id) {
		return storeEmployeeTaskDao.findOne(id);
	}

	public List<StoreEmployeeTask> find(StoreEmployeeTask storeEmployeeTask, String status,
			String finished, String employeeName, String taskDate) {
		DetachedCriteria dc = storeEmployeeTaskDao.createDetachedCriteria();
		if (storeEmployeeTask.getStore() != null
				&& storeEmployeeTask.getStore().getId() != null) {
			dc.add(Restrictions.eq("store.id", storeEmployeeTask.getStore()
					.getId()));
		}
		int statusInt = 0;
		if (status != null) {
			statusInt = Integer.parseInt(status);
			if (statusInt != -1) {
				dc.add(Restrictions.eq("viewed", statusInt));
			}
		}
		int finishedInt = 0;
		if (finished != null) {
			finishedInt = Integer.parseInt(finished);
			if (finishedInt != -1) {
				dc.add(Restrictions.eq("completed", finishedInt));
			}
		}
		if (taskDate != null && !"".equals(taskDate)) {
			Date date = DateUtils.parseDate(taskDate);
			Date begin = DateUtils.getBeginOfDay(date);
			Date end = DateUtils.getEndOfDay(date);
			dc.add(Restrictions.between("taskDate", begin, end));
		}
		dc.createAlias("employee", "e");
		if (employeeName != null) {
			dc.add(Restrictions.like("e.name", "%" + employeeName + "%"));
		}
		//只显示当前时间以前的任务单
		dc.add(Restrictions.le("taskDate", DateUtils.getEndOfDay(new Date())));
		dc.addOrder(Order.desc("taskDate"));
		return storeEmployeeTaskDao.find(dc);
	}

	@Transactional(readOnly = false)
	public List<StoreEmployeeTask> findByStoreReserve(Long storeReserveId) {
		return storeEmployeeTaskDao.findByStoreReserve(storeReserveId);
	}
	@Transactional(readOnly = false)
	public void changeStatus(StoreEmployeeTask storeEmployeeTask) {
		int status = storeEmployeeTask.getViewed();
		if (status == StoreEmployeeTask.STATUS_NOT_VIEWED) {
			storeEmployeeTask.setViewed(StoreEmployeeTask.STATUS_VIEWED);
		}
		if (status == StoreEmployeeTask.STATUS_VIEWED) {
			storeEmployeeTask.setViewed(StoreEmployeeTask.STATUS_NOT_VIEWED);
		}
		save(storeEmployeeTask);
	}

	@Transactional(readOnly = false)
	public void changeToFinished(StoreEmployeeTask storeEmployeeTask) {
		int finished = storeEmployeeTask.getCompleted();
		if (finished == StoreEmployeeTask.STATUS_NOT_COMPLETED) {
			storeEmployeeTask.setCompleted(StoreEmployeeTask.STATUS_COMPLETED);
			save(storeEmployeeTask);
		}
	}

	@Transactional(readOnly = false)
	public List<StoreEmployeeTask> findUnreadMsg(Long storeId) {
		DetachedCriteria dc = storeEmployeeTaskDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store.id", storeId));
		dc.add(Restrictions.eq("viewed", StoreEmployeeTask.STATUS_NOT_VIEWED));
		return storeEmployeeTaskDao.find(dc);
	}

	@Transactional(readOnly = false)
	public List<StoreEmployeeTask> findUnfinishedMsg(Long storeId) {
		DetachedCriteria dc = storeEmployeeTaskDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store.id", storeId));
		dc.add(Restrictions.eq("completed",
				StoreEmployeeTask.STATUS_NOT_COMPLETED));
		return storeEmployeeTaskDao.find(dc);
	}

	@Transactional(readOnly = false)
	public void save(StoreEmployeeTask storeEmployeeTask) {
		storeEmployeeTaskDao.clear();
		storeEmployeeTaskDao.save(storeEmployeeTask);
	}
	@Transactional(readOnly = false)
	public void delete(StoreEmployeeTask storeEmployeeTask) {
		storeEmployeeTaskDao.delete(storeEmployeeTask);
	}
}
